package com.example.shoppingdemo.nslcommodity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.example.shoppingdemo.R;
import com.example.shoppingdemo.nslcommodity.citybean.CityData;
import com.example.shoppingdemo.nslcommodity.citybean.CityList;
import com.example.shoppingdemo.nslcommodity.citybean.CityModel;
import com.example.shoppingdemo.nslcommodity.citybean.CityModel2;
import com.example.shoppingdemo.nslcommodity.citybean.CityModel3;
import com.example.shoppingdemo.nslcommodity.citybean.CityPerson;
import com.example.shoppingdemo.nslcommodity.citybean.Citymodel1;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class CityActivity extends AppCompatActivity {
    List<Citymodel1> citymodel1s;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city);
        initData();
    }
    private void initData(){
        try {
            InputStream is= getAssets().open("City.json");
            BufferedReader reader=new BufferedReader(new InputStreamReader(is));
            StringBuffer sb=new StringBuffer();
            String s="";
            while (null!=(s=reader.readLine())){
                sb.append(s);
            }
            is.close();
            reader.close();
            CityPerson person=
                    new Gson().fromJson(sb.toString(),CityPerson.class);
            Observable.create(new Observable.OnSubscribe<CityPerson>(){
                @Override
                public void call(Subscriber<? super CityPerson> subscriber) {
                    subscriber.onNext(person);
                }
            }).map(new Func1<CityPerson, List<Citymodel1>>() {
                @Override
                public List<Citymodel1> call(CityPerson cityPerson) {
                    citymodel1s=new ArrayList<>();
                    for (int i = 0; i <cityPerson.getData().length ; i++) {
                        Citymodel1 citymodel1=new Citymodel1();
                        citymodel1.setName(cityPerson.getData()[i].getName());
                        List<CityModel3> cityModel3s=new ArrayList<>();
                        for (int j = 0; j <cityPerson.getData()[i].getCity().length ; j++) {
                            CityModel3 model3=new CityModel3();
                            model3.setName(cityPerson.getData()[i].getCity()[j].getName());
                            List<CityModel> cityModels=new ArrayList<>();
                            for (int k = 0; k <cityPerson.getData()[i].getCity()[j].getErea().length ; k++) {
                                CityModel model=new CityModel();
                                model.setName(cityPerson.getData()[i].getCity()[j].getErea()[k]);
                                cityModels.add(model);
                            }
                            model3.setXian(cityModels);
                            cityModel3s.add(model3);
                        }
                        citymodel1.setShi(cityModel3s);
                        citymodel1s.add(citymodel1);
                    }
                    return citymodel1s;
                }
            }).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<List<Citymodel1>>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(List<Citymodel1> citymodel1List) {

                        }
                    });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void spanner(){

    }
}